const app = getApp()
Page({
  data: {
    oldPas: '',
    newPas: '',
    conPas: ''
  },
  inpOld: function(e) {
    this.setData({
      oldPas: e.detail.value
    })
  },
  inpPas: function(e) {
    this.setData({
      newPas: e.detail.value
    })
  },
  inpConpas: function(e) {
    this.setData({
      conPas: e.detail.value
    })
  },
  funConfirm: function() {
    const that = this
    if (that.data.oldPas == '') {
      wx.showToast({
        title: '请输入原始密码！',
        icon: 'none',
        duration: 2000
      })
    } else if (that.data.newPas == '') {
      wx.showToast({
        title: '请输入新密码！',
        icon: 'none',
        duration: 2000
      })
    } else if (that.data.newPas == that.data.conPas && that.data.newPas != that.data.oldPas) {
      wx.cloud.callFunction({
        name: 'changePas',
        data: {
          userType: app.globalData.userType,
          userId: app.globalData.userId,
          oldPas: that.data.oldPas,
          newPas: that.data.newPas
        }
      }).then(res => {
        if (res.result.result == 0) {
          wx.showToast({
            title: '原始密码输入错误！',
            icon: 'none',
            duration: 2000
          })

        } else if (res.result.result == 1) {
          wx.showModal({
            title: '提示',
            content: '密码修改成功，请重新登录',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.reLaunch({
                  url: '../login/login',
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: '修改密码失败！',
            icon: 'none',
            duration: 2000
          })
        }
      })
    } else if (that.data.newPas != that.data.conPas) {
      wx.showToast({
        title: '新密码两次输入不一致！',
        icon: 'none',
        duration: 2000
      })
    } else if (that.data.newPas == that.data.oldPas) {
      wx.showToast({
        title: '新密码和旧密码不能相同！',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.showToast({
        title: '修改密码失败！',
        icon: 'none',
        duration: 2000
      })
    }
  }
})